package edu.java.lectures.lec11.threads.basics.creation;

public class MyRunnableTest {

	public static void main(String[] args) {
		// initializing of the concrete tasks
		MyRunnable penchoTask = new MyRunnable("pencho");
		MyRunnable minchoTask = new MyRunnable("mincho");

		// initializing of the corresponding threads
		Thread penchoThread = new Thread(penchoTask, "penchoThread");
		Thread minchoThread = new Thread(minchoTask, "minchoThread");

		penchoThread.setPriority(Thread.MAX_PRIORITY);

		System.out.println("Pencho tries to cheat...");
		penchoThread.run();

		System.out.println("Starting threads...");
		// starting the threads
		penchoThread.start();
		minchoThread.start();

		try {
			// 1) mincho main work simultaneously
			// 2) main calls minchoThread.join()
			// 3) main stops the execution at the point of the join()
			// invocation
			// 4) mincho finishes its work
			// 5) main goes on with its own execution on the line after the
			// join() method invocation
			minchoThread.join();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		System.out.println("Main thread goes to sleep 20 seconds...");
		try {
			// 1 second = 1000 milliseconds
			Thread.sleep(5000); // applied to the CURRENTLY EXECUTED THREAD
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Main thread awaiked.");
		System.out.println("Pencho's thread is alive: "
				+ penchoThread.isAlive());
		penchoThread = null;
		// System.gc();
		// penchoThread.start();
		System.out.println("Mincho's thread is alive: "
				+ minchoThread.isAlive());
		System.out.println("main thread dies...");
	}
}
